﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using log4net;
using MMS.DAO;
using System.Globalization;
using MMS.Validation;

namespace MMS
{
    public partial class FormMaterialInfo : Form
    {

        private MaterialDTO material;
        public bool istxtPriceByDayActive = false;
        UnitDAO unitDao = new UnitDAO(Param.ConnectionString.Value);
        MaterialDAO materialDAO = new MaterialDAO(Param.ConnectionString.Value);

        public FormMaterialInfo()
        {
            InitializeComponent();
            this.Text = Utils.APP_TITLE;
            material = new MaterialDTO();
            material.Id = "";
            PopulateAllUnit();
        }

        public FormMaterialInfo(MaterialDTO materialDTO)
        {
            InitializeComponent();
            this.Text = Utils.APP_TITLE;
            try
            {
                this.material = materialDTO;
                PopulateAllUnit();populateMaterialInfo();
                bbiSave.Visibility = BarItemVisibility.Never;
            }
            catch (Exception e)
            {
                LogUtil.log.Error(e);
            }
        }

        private void populateMaterialInfo()
        {
            txtId.Text = material.Id;
            txtName.Text = material.Name;
            txtPriceByDay.Text = material.PriceByDay.ToString();
            UnitDTO unitDTO = unitDao.selectFromName(material.UnitName);lueUnit.EditValue = unitDTO.Id;
            txtDescription.Text = material.Description;

        }

        private void getMaterialInfoFromUI()
        {
             material.Name=   txtName.Text  ;
             material.PriceByDay= Int32.Parse(txtPriceByDay.Text )   ;
            material.IdUnit = ((UnitDTO) lueUnit.GetSelectedDataRow()).Id;
             material.Description=    txtDescription.Text ;

        }

        private void saveMaterial()
        {
            // material.Id= txtId.Text   ;
            material.Name = txtName.Text;
            material.PriceByDay = Int32.Parse(ValidateUtil.removeCommaFromString(txtPriceByDay.Text));

            //unitDao.selectFromName()
            material.IdUnit = ((UnitDTO)lueUnit.GetSelectedDataRow()).Id;
            material.Description = txtDescription.Text;

            if (material.Id == "")
            {
                materialDAO.Insert(material);
            }
            else
            {
                materialDAO.Update(material);
            }
        }

        private void FormMaterialInfo_Load(object sender, EventArgs e)
        {

        }

        private void PopulateAllUnit()
        {
            UnitDAO unitDAO = new UnitDAO(Param.ConnectionString.Value);
            List<UnitDTO> unitList = unitDAO.SelectAll();
            this.lueUnit.Properties.DataSource = unitList;
            this.lueUnit.Properties.DisplayMember = "UnitName";
            this.lueUnit.Properties.ValueMember = "Id";
            lueUnit.Properties.Columns[0].FieldName = "Id";
            lueUnit.Properties.Columns[1].FieldName = "UnitName";
            lueUnit.Properties.BestFitMode = DevExpress.XtraEditors.Controls.BestFitMode.BestFitResizePopup;
            lueUnit.Properties.SearchMode = DevExpress.XtraEditors.Controls.SearchMode.AutoComplete;
            lueUnit.Properties.AutoSearchColumnIndex = 1;
        }

        private void clearAll()
        {
            txtName.Text = "";
            txtDescription.Text = "";
            txtPriceByDay.Text = "";
        }

        private void txtPriceByDay_KeyPress(object sender, KeyPressEventArgs e)
        {
            ValidateUtil.isDigit(e);
        }

        private void txtPriceByDay_TextChanged(object sender, EventArgs e)
        {

            txtPriceByDay.Text = ValidateUtil.formatNum(txtPriceByDay.Text);

            txtPriceByDay.SelectionStart = txtPriceByDay.Text.Length;
        }

        private void barButtonItem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string senderName = e.Item.Name;
            if (senderName.Equals(bbiSave.Name))
            {
                if (ValidateFormMaterialInfo.validateUI(this))
                {
                    saveMaterial();
                    clearAll();
                }
            }
            else if (senderName.Equals(bbiSaveAndExit.Name))
            {
                if (ValidateFormMaterialInfo.validateUI(this))
                {
                    saveMaterial();
                    this.Close();
                }
            }
            else if (senderName.Equals(bbiAddUnit.Name))
            {
                FormUnitInfo f = new FormUnitInfo();
                f.Text = MessageBundle.ADD_NEW_UNIT.Value;
                f.ShowDialog();
                PopulateAllUnit();
            }
            else if (senderName.Equals(bbiExit.Name))
            {
                this.Close();
            }
        }

    }
}
